import Translate from "./translate";

const validateUserName = (rule, value, callback) => {
  if ((/^[a-zA-Z_0-9]{0,}$/).test(value)) {
    callback();
  } else {
    callback(new Error("请输入英文字母、下划线、数字。"));
  }
};
const validatePassword = (rule, value, callback) => {
  if ((/^[a-zA-Z_0-9]{0,}$/).test(value)) {
    callback();
  } else {
    callback(new Error("请输入英文字母、下划线、数字。"));
  }
};

const validateZhName = (rule, value, callback) => {
  if ((/^[\u4e00-\u9fa5]{0,}$/).test(value)) {
    callback();
  } else {
    callback(new Error(Translate.translate("Please input Chinese!")));
  }
};

const validatePhone = (rule, value, callback) => {
  if ((/^1[3-9][0-9]{9}$/).test(value)) {
    callback();
  } else {
    callback(new Error("无效手机号"));
  }
};
const validateAmount = (rule, value, callback) => {
  if (value > 0) {
    callback();
  } else {
    callback(new Error("输入错误！"));
  }
};
// const validateCheck = (rule, value, callback) => {
//   if (value != 0) {
//     callback();
//   } else {
//     callback(new Error("请审核"));
//   }
// };
const validateEmail = (rule, value, callback) => {
  if ((/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/).test(value)) {
    callback();
  } else {
    callback(new Error("邮箱地址格式错误！"));
  }
};
const validateIdCardNo = (rule, value, callback) => {
  if (
    !(/^([1-6][1-9]|50)\d{4}(19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/).test(
      value
    )
  ) {
    return callback(new Error("无效身份证号"));
  }
  let birthYear = value.slice(6, 10);
  let nowDate = new Date();
  let age = nowDate.getFullYear() - birthYear;

  console.log(age);
  switch (true) {
    case age < 18 || age > 65:
      return callback(new Error("年龄限制：18岁至65岁之间"));
    case age == 18: {
      let birthMonth = value.slice(10, 12);
      let difference = nowDate.getMonth() + 1 - birthMonth;

      if (difference < 0) {
        return callback(new Error("年龄限制：不可小于18岁"));
      } else if (difference == 0) {
        let birthDay = value.slice(12, 14);
        difference = nowDate.getDate() - birthDay;
        if (difference <= 0) {
          return callback(new Error("年龄限制：不可小于18岁"));
        }
      }
      break;
    }
    case age == 65: {
      let birthMonth = value.slice(10, 12);
      let difference = nowDate.getMonth() + 1 - birthMonth;

      if (difference > 0) {
        callback(new Error("年龄限制：不可大于65岁"));
      } else if (difference == 0) {
        let birthDay = value.slice(12, 14);
        difference = nowDate.getDate() - birthDay;
        if (difference >= 0) {
          callback(new Error("年龄限制：不可大于65岁"));
        }
      }
      break;
    }
  }
  return callback();
  // if (age < 18 && age > 65) {
  //   callback(new Error("年龄限制：18岁至65岁之间"));
  // } else if (age == 18) {
  //   let birthMonth = value.slice(10, 12);
  //   let difference = nowDate.getMonth() + 1 - birthMonth;

  //   if (difference < 0) {
  //     callback(new Error("年龄限制：不可小于18岁"));
  //   } else if (difference == 0) {
  //     let birthDay = value.slice(12, 14);
  //     difference = nowDate.getDate() - birthDay;
  //     if (difference <= 0) {
  //       callback(new Error("年龄限制：不可小于18岁"));
  //     }
  //   }
  // } else if (age == 65) {
  //   let birthMonth = value.slice(10, 12);
  //   let difference = nowDate.getMonth() + 1 - birthMonth;

  //   if (difference > 0) {
  //     callback(new Error("年龄限制：不可大于65岁"));
  //   } else if (difference == 0) {
  //     let birthDay = value.slice(12, 14);
  //     difference = nowDate.getDate() - birthDay;
  //     if (difference >= 0) {
  //       callback(new Error("年龄限制：不可大于65岁"));
  //     }
  //   }
  // } else {
  //   return callback();
  // }
};
const validateWorkContent = (rule, value, callback) => {
  let sensitiveWords = [
    "工资",
    "补贴",
    "津贴",
    "福利",
    "劳务",
    "销售",
    "经纪",
    "工程安装",
    "装修",
    "清包工",
  ];
  // sensitiveWords.forEach((v) => {
  //   if (value.indexOf(v) != -1) {
  //     callback(new Error(`工作内容中不能包含${v}`));
  //     return;
  //   }
  // });

  if (
    sensitiveWords.every((v) => {
      if (value.indexOf(v) != -1) {
        callback(new Error(`工作内容中不能包含${v}`));
        return false;
      } else {
        return true;
      }
    })
  ) {
    callback();
  }
};
const validateSqlInjection = (rule, value, callback) => {
  let reg = new RegExp(
    "and|exec|execute|insert|select|delete|update|chr|mid|master|char|declare|sitename|net user|xp_cmdshell|or|create|drop|table|from|grant|use|group_concat|column_name|information_schema.columns|table_schema|union|where|select|delete|update|orderhaving|having|by|truncate|like",
    "i"
  );
  let array = reg.exec(value);
  if (array) {
    callback(new Error(`输入了敏感词,${array.join(",")}`));
  } else {
    callback();
  }
};
let amountFields = {
  CanrefundAmount: "可退款金额",
  Amount: "",
  refundAmount: "退款金额",
  refundRechargeAmount: "应退金额（含服务费）",
  RechargeAmount: "",
  // _rechargetime: "充值时间",
  AccountAmount: "",
  PayMaxAmount: 93500,
  MonthAcceptanceMaxAmount: 2000000,
  YearAcceptanceMaxAmount: 5000000,
  InvoiceTaxAmount: "",
  CanAcceptanceAmount: "",
};
let requiredFields = {
  "payeeInfo.identityType": "",
  "payeeInfo.name": "",
  "payeeInfo.identity": "",
  transAmount: "",
  orderTitle: "",
  areaName: "",
  TskType: "",
  taskRegionC: "",
  excelUrl: "",
  customerReceiptUrl: "",
  TSKSettlementType: "",
  TskEmpFile: "",
  confirmVCode: "",
  IsEnabled: "",
  customCollector: "",
  InvoiceCode: "",
  InvoiceNumber: "",
  wlgs: "",
  wldh: "",
  ServiceContent: "",
  SuperiorAgentName: "",
  customerManager: "",
  AgentName: "",
  USCC: "",
  Address: "",
  LegalPersonName: "",
  IdNumber: "",
  ContactName: "",
  Region: "",
  SettlementBank: "",
  SettlementAccount: "",
  Rate: null,
  QZDRate: null,
  AgentLevel: null,
  BusinesslicenseImg: null,
  identitycardImg: null,
  identitycardBackImg: null,
  AID: "",
  WebUrl: "string",
  WebSystemName: "string",
  smsSignName: "string",
  WebBA: "string",
  icoUrl: "string",
  WebLogoUrl: "string",
  WebBackImgUrl: "string",
  LoginLogoUrl: "string",
  FilePath: "",
  ruleType: "",
  ServiceBank: "充值银行",
  ServiceAccountNo: "充值账号",
  refundType: "退款类型",

  refundReason: "退款说明",
  AttFileUrl: "申请函附件",
  ServiceProviderName: "",
  // ServiceBankId: null,
  PayChannelId: "",

  // MID: "客户",
  CustomerName: "",
  // _agentname: "客户名称",
  RechargeRate: "",
  RechargeTime: "",

  PayingAccountName: "",
  PayingBank: "",
  PayingAccount: "",

  MSignstatus: "",
  verifybankcard: "",
  verifybankcardType: "",
  RecipientName: "",
  RecipientAddr: "",
  IsDefault: "",
  Invoicesubject: "",

  selectedDate: "",
  Month: "",
  role_id: 0,
  // role_type: 0,
  // user_name: "string",
  BranchName: "string",

  // busnieseNum: "string",
  // type: 0,
  password: "string",
  // salt: "string",
  empname: "",
  bankcardno: "",
  idcardno: "",
  phone: "",
  title: "",
  roleId: "",
  Sex: "",
  ServiceID: "",
  AccountBank: "",
  BankNo: "",
  ServiceId: "",
  sid: "",
  ServiceType: "",
  recruitPerson: "",
  SettlementModes: "",
  Reason: "",
  ExcelUrl: "",
  SID: "",
  paytype: "",
  SignatureRate: "",
  RecruitmentRequirements: "",
  otherSettlementModes: "",
  RequirementDescription: "",
  attFile: "",
  fileList: "",
  MerchantName: "",
  AgentId: "",
  ServiceContents: "",
  customLegalPerson: "",
  customerName: "",
  customTaxIdcd: "",
  MID: "",
  ServiceName: "",
  BegTime: "",
  EndTime: "",
  BeginDate: "",
  EndDate: "",
  TechnicalRequirement: "",
  Companytype: "",
  invoiceType: "",
  empSignType: "",
  customNature: "",
  payType: "",
  IsSendMessageToEmp: "",
};
const wordFields = {
  WorkContent: "",
  PostName: "",
  ProjectName: "",
  projectName: "",
  TechnicalRequirement: "",
  RequirementDescription: "",
};
const rules = {
  name: [
    { required: true, message: "不能为空", trigger: "blur" },
    { min: 4, message: "长度不可小于 4 个字符", trigger: "blur" },
    { max: 30, message: "长度不可大于 30 个字符", trigger: "blur" },
    { validator: validateUserName, trigger: "blur" },
  ],
  user_name: [
    { required: true, message: "不能为空", trigger: "blur" },
    { min: 4, message: "长度不可小于 4 个字符", trigger: "blur" },
    { max: 30, message: "长度不可大于 30 个字符", trigger: "blur" },
    { validator: validateUserName, trigger: "blur" },
  ],

  real_name: [
    { required: true, message: "不能为空", trigger: "blur" },
    { min: 2, message: "长度不可小于 2 个字符", trigger: "blur" },
    { max: 5, message: "长度不可大于 5 个字符", trigger: "blur" },
    { validator: validateZhName, trigger: "blur" },
  ],
  userName: [
    { required: true, message: "不能为空", trigger: "blur" },
    { min: 4, message: "长度在 不小于 4 个字符", trigger: "blur" },
    { max: 15, message: "长度在 不大于 15 个字符", trigger: "blur" },
    { validator: validateUserName, trigger: "blur" },
  ],
  password: [
    { required: true, message: "不能为空", trigger: "blur" },
    { min: 6, message: "长度在 不小于 6 个字符", trigger: "blur" },
    { max: 15, message: "长度在 不大于 15 个字符", trigger: "blur" },
    { validator: validatePassword, trigger: "blur" },
  ],

  employee: {
    idCardNo: [
      { required: true, message: "不能为空", trigger: "blur" },
      { validator: validateIdCardNo, trigger: "blur" },
    ],
  },
  EmpName: [
    { required: true, message: "不能为空", trigger: "blur" },
    { min: 2, message: "长度不可小于 2 个字符", trigger: "blur" },
    { max: 5, message: "长度不可大于 5 个字符", trigger: "blur" },
    { validator: validateZhName, trigger: "blur" },
  ],
  IdCardNo: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validateIdCardNo, trigger: "blur" },
  ],
  Merchant: [{ required: true, message: "不能为空", trigger: "blur" }],
  BankName: [{ required: true, message: "不能为空", trigger: "blur" }],
  BankCardNo: [{ required: true, message: "不能为空", trigger: "blur" }],
  EmpPhone: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validatePhone, trigger: "blur" },
  ],
  Phone: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validatePhone, trigger: "blur" },
  ],
  ReservedPhone: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validatePhone, trigger: "blur" },
  ],
  telephone: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validatePhone, trigger: "blur" },
  ],
  customCollectorPhone: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validatePhone, trigger: "blur" },
  ],
  customMail: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validateEmail, trigger: "blur" },
  ],
  ServiceContentId: [{ required: true, message: "不能为空", trigger: "blur" }],

  IdNumber: [
    { required: true, message: "不能为空", trigger: "blur" },
    { validator: validateIdCardNo, trigger: "blur" },
  ],
  CheckStatus: [
    { required: true, message: "不能为空", trigger: "blur" },
    // { validator: validateCheck, trigger: "blur" },
  ],
};
function addField(params) {
  let keys = Object.keys(params);
  for (let i = 0; i < keys.length; i++) {
    rules[keys[i]] = [
      { required: true, message: "不能为空", trigger: "blur" },
      { validator: validateSqlInjection, trigger: "blur" },
    ];
  }
}
function addAmountField(params) {
  let keys = Object.keys(params);
  for (let i = 0; i < keys.length; i++) {
    rules[keys[i]] = [
      { required: true, message: "不能为空", trigger: "blur" },
      // { type: "number", message: "必须为数字值" },
      { validator: validateAmount, trigger: "blur" },
    ];
  }
}
function addWorkContent(params) {
  for (const key in params) {
    if (Object.hasOwnProperty.call(params, key)) {
      rules[key] = [
        { required: true, message: "不能为空", trigger: "blur" },
        // { type: "number", message: "必须为数字值" },
        { validator: validateWorkContent, trigger: "blur" },
      ];
    }
  }
}
addField(requiredFields);
addAmountField(amountFields);
addWorkContent(wordFields);
export default rules;
